package com.king;

import java.util.*;

/**
 * @Author: KingWang
 * @Date: 2021/5/22
 * @Desc:
 **/
public class SortTest {

    public static void main(String[] args) {

        demo();

    }

    private static void demo() {
        SortTest sort = new SortTest();
        List<User> list = sort.getList();
        Collections.sort(list, Comparator.comparingInt(o -> o.sort));
        for(User user:list){
            System.out.println(user.name + "," + user.age + "," + user.sort);
        }
    }

    public List<User> getList(){
        Random random = new Random();
        List<User> users = new ArrayList<>();
        for (int i = 0; i < 10 ; i++) {
            User user = new User("king",random.nextInt(10) * 2 + 20,random.nextInt(10));
            users.add(user);
        }
        return users;
    }

    class User{
        private String name;
        private Integer age;
        private Integer sort;

        public User(String name, Integer age, Integer sort) {
            this.name = name;
            this.age = age;
            this.sort = sort;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", sort=" + sort +
                    '}';
        }
    }
}
